﻿using System;
using System.IO;
using System.Reflection;
using __ts.Core.Log;

namespace __ts.Core.Plugin
{
    /// <summary>
    /// 
    /// </summary>
    public class RemoteLoader : MarshalByRefObject
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="assemblyPath"></param>
        /// <returns></returns>
        public IPlugin Install(string assemblyPath)
        {
            try
            {
                Assembly _assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyPath));
                string assemblyName = Path.GetFileNameWithoutExtension(assemblyPath);
                string classPath = assemblyName + ".Plugin";
                Type type = _assembly.GetType(classPath);

                return (IPlugin)type.InvokeMember("GetInstance",
                    BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.GetProperty,
                    null,
                    null,
                    new object[] { });

                //return (IPlugin)Activator.CreateInstance(type);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
            return null;
        }
        /// <summary>
        /// 远程对象生命周期
        /// </summary>
        /// <returns></returns>
        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
}
